<?php

namespace App\Module\Sub;

use App\depository\SubCfgDep;
use App\Module\BaseModule;
use App\Service\DictService;
use App\depository\CfgDep;

class SubCfgModule extends BaseModule
{
    public function init($request)
    {

        $param = $request->input();

        $dictService = new DictService();
        $dictService->setUsers()->setTeamStatus()->setDictPermission();

        $data = [
            'dict' => $dictService->getDict(),
        ];

        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        if (empty($param['name'])) {
            return self::response([], '请输入文件名', 100);
        }

        if (empty($param['content'])) {
            return self::response([], '请输入文件内容', 100);
        }

        $dep = new SubCfgDep();
        $data = [
            'team_id' => $teamId,
            'name' => $param['name'],
            'content' => $param['content'],
            'remark' => $param['remark'] ?? ""
        ];
        $dep->add($data);


        return self::response();
    }

    public function copy($request){

        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        $serverDep = new CfgDep();
        $resServer = $serverDep->first($param['id']);

        $subDep = new SubCfgDep();
        $resDetail = $subDep->firstByName($teamId,$resServer['name']);
        if($resDetail){
            return self::response([],'请勿重复复制',200);
        }

        $data = [
            'team_id' => $teamId,
            'name' => $resServer['name'],
            'content' => $resServer['content'],
            'remark' => $resServer['remark']
        ];

        $dep = new SubCfgDep();
        $dep->add($data);

        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new SubCfgDep();

        $dep->del($param['id']);
        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        if (empty($param['name'])) {
            return self::response([], '请输入文件名', 100);
        }

        if (empty($param['content'])) {
            return self::response([], '请输入文件内容', 100);
        }

        $dep = new SubCfgDep();
        $data = [
          'name' => $param['name'],
          'content' => $param['content'],
          'remark' => $param['remark']
        ];
        $dep->upd($param['id'],$data);

        return self::response();

    }

    public function list($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        $dep = new SubCfgDep();
        $resList = $dep->list($teamId,$param);

        $data['list'] = $resList->map(function ($item) {
            return [
                'id' => $item->id,
                'name' => $item->name,
                'remark' => $item->remark,
//                'content' => $item->content,
                'created_at' => $item->created_at->toDatetimeString(),
            ];
        });

        return self::response($data);
    }

    public function detail($request)
    {
        $param = $request->input();

        $dep = new SubCfgDep();

        $resDetail = $dep->first($param['id']);
        $data['detail'] = [
            'id' => $resDetail->id,
            'name' => $resDetail->name,
            'remark' => $resDetail->remark,
            'content' => $resDetail->content,
            'created_at' => $resDetail->created_at->toDatetimeString()
        ];

        return self::response($data);
    }
}

